home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 194_01 / compress.c < prev    next >
Text File  |  1985-11-13  |  1KB  |  70 lines

  1. /* [COMPRESS.C of JUGPDS Vol.17]
  2. *****************************************************************
  3. *                                *
  4. *    Written by Muneaki Chiba                 *
  5. *    Edited by Hakuo Katayose (JUG-CP/M No.179)        *
  6. *            49-114 Kawauchi-Sanjuunin-machi        *
  7. *            Sendai, Miyagi 980                          *
  8. *            Phone: 0222-61-3219                *
  9. *                                *
  10. *    Tested by Y. Monma (JUG-C/M Disk Editor)                * 
  11. *                                *
  12. *****************************************************************
  13. */
  14.  
  15. #include "stdio.h"
  16. #include <dio.h>
  17.  
  18. #define    RCODE        127
  19. #define    MAXCHUNK    (RCODE-'0'-1)
  20. #define    THRESH        5
  21.  
  22. main(argc, argv)
  23. int    argc;
  24. char *argv[];
  25.  
  26. {
  27.     int    c, lastc, nrep, nsave;
  28.     char    buff[MAXCHUNK];
  29.  
  30.     dioinit(&argc, argv);
  31.     if (argc < 2)
  32.         error("Usage: cmprs <infile >outfile ^Z");
  33.     nsave = 0;
  34.     for (lastc = getchar(); lastc != EOF; lastc = c) {
  35.         for (nrep = 1; (c = getchar()) == lastc; nrep++)
  36.             if (nrep >= MAXCHUNK)
  37.                 break;
  38.         if (nrep < THRESH)
  39.             while (nrep--) {
  40.                 buff[nsave++] = lastc;
  41.                 if (nsave >= MAXCHUNK)
  42.                     putbuf(buff, &nsave);
  43.                 }
  44.         else {
  45.             putbuf(buff, &nsave);
  46.             putchar(RCODE);
  47.             putchar(lastc);
  48.             putchar(nrep+'0');
  49.              }
  50.     }
  51.     putbuf(buff, &nsave);
  52.     dioflush();
  53. }
  54.  
  55.  
  56. putbuf(p, nsave)
  57. char    *p;
  58. int    *nsave;
  59.  
  60. {
  61.     int    i;
  62.  
  63.     if (*nsave) {
  64.         putchar(*nsave + '0');
  65.         while ((*nsave)--)
  66.             putchar(*p++);
  67.      }
  68.     *nsave = 0;
  69. }
  70.